package com.sys.controller;

import com.sys.dto.PlanItemWithTaskDTO;
import com.sys.dto.PlanRequest;
import com.sys.entity.BizPlanItem;
import com.sys.service.PlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/assign")
public class PlanController {

    @Autowired
    private PlanService planService;

    @PostMapping("/save")
    public Map<String, Object> savePlan(@RequestBody PlanRequest planRequest) {
        planService.savePlan(planRequest);
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("msg", "保存成功");
        return response;
    }

    @GetMapping("/daily")
    public List<PlanItemWithTaskDTO> getDailyTasks(@RequestParam Long userId) {
        return planService.getDailyTasks(userId);
    }

    @DeleteMapping("/plan-item/{planItemId}")
    public Map<String, Object> deletePlanItem(
            @PathVariable Long planItemId,
            @RequestParam Long userId) {
        planService.deletePlanItem(planItemId, userId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("msg", "删除成功");
        return response;
    }

    @GetMapping("/plans")
    public List<com.sys.entity.BizPlan> getMyPlans(@RequestParam Long userId) {
        return planService.getPlansByUserId(userId);
    }

    @DeleteMapping("/plan/{planId}")
    public Map<String, Object> deletePlan(@PathVariable Long planId) {
        planService.deletePlan(planId);
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        response.put("msg", "删除成功");
        return response;
    }

    @GetMapping("/plan/{planId}")
    public java.util.Map<String, Object> getPlanDetail(@PathVariable Long planId) {
        return planService.getPlanDetailWithTasks(planId);
    }
}